#!/usr/bin/python

import calendar
import string
import re
import datetime
from pepe.p_datetime.basic import isWeekend

# do not read it from external file because each call of function would do it
# it decreases performance of function hugely
#holidays_s = re.sub('\t', '', open('data.txt','r').read())

# if it is inside of function, it is compiled into bitecode and loaded only once when imported
TXT_HOLIDAYS = """#
# added 20091005-0052 by vip
cz	2008-01-01
cz	2008-03-23
cz	2008-03-24
cz	2008-05-01
cz	2008-05-08
cz	2008-07-05
cz	2008-07-06
cz	2008-09-28
cz	2008-10-28
cz	2008-11-17
cz	2008-12-24
cz	2008-12-25
cz	2008-12-26
cz	2009-01-01
cz	2009-04-12
cz	2009-04-13
cz	2009-05-01
cz	2009-05-08
cz	2009-07-05
cz	2009-07-06
cz	2009-09-28
cz	2009-10-28
cz	2009-11-17
cz	2009-12-24
cz	2009-12-25
cz	2009-12-26
cz      2010-01-01
cz      2010-04-05
cz      2010-05-01
cz      2010-05-08
cz      2010-07-05
cz      2010-07-06
cz      2010-09-28
cz      2010-10-28
cz      2010-11-17
cz      2010-12-24
cz      2010-12-25
cz      2010-12-26
sk	2008-01-01
sk	2008-01-06
sk	2008-03-21
sk	2008-03-24
sk	2008-05-01
sk	2008-05-08
sk	2008-07-05
sk	2008-08-29
sk	2008-09-01
sk	2008-09-15
sk	2008-11-01
sk	2008-11-17
sk	2008-12-24
sk	2008-12-25
sk	2008-12-26
sk	2009-01-01
sk	2009-01-06
sk	2009-04-14
sk	2009-04-17
sk	2009-05-01
sk	2009-05-08
sk	2009-07-05
sk	2009-08-29
sk	2009-09-01
sk	2009-09-05
sk	2009-11-01
sk	2009-11-17
sk	2009-12-24
sk	2009-12-25
sk	2009-12-26
sk	2010-01-01
sk	2010-07-05
sk	2010-08-29
sk	2010-09-01
sk	2010-11-17
sk	2010-01-06
sk	2010-05-01
sk	2010-05-08
sk	2010-09-15
sk	2010-11-01
sk	2010-12-24
sk	2010-12-25
sk	2010-12-26
sk	2010-04-05
sk	2010-04-02
hu	2007-01-01
hu	2007-03-15
hu	2007-04-08
hu	2007-04-09
hu	2007-05-01
hu	2007-05-28
hu	2007-08-20
hu	2007-10-23
hu	2007-11-01
hu	2007-12-24
hu	2007-12-25
hu	2007-12-26
hu	2008-01-01
hu	2008-03-15
hu	2008-03-23
hu	2008-03-24
hu	2008-05-01
hu	2008-05-12
hu	2008-08-20
hu	2008-10-23
hu	2008-11-01
hu	2008-12-24
hu	2008-12-25
hu	2008-12-26
hu	2009-01-01
hu	2009-03-15
hu	2009-04-12
hu	2009-04-13
hu	2009-05-01
hu	2009-06-01
hu	2009-08-20
hu	2009-10-23
hu	2009-11-01
hu	2009-12-25
hu	2009-12-26
pl	2007-01-01
pl	2007-04-08
pl	2007-04-09
pl	2007-05-01
pl	2007-05-03
pl	2007-05-28
pl	2007-06-07
pl	2007-08-15
pl	2007-11-01
pl	2007-11-11
pl	2007-12-25
pl	2007-12-26
pl	2008-01-01
pl	2008-03-23
pl	2008-03-24
pl	2008-05-01
pl	2008-05-03
pl	2008-05-22
pl	2008-05-11
pl	2008-08-15
pl	2008-11-01
pl	2008-11-11
pl	2008-12-25
pl	2008-12-26
pl	2009-01-01
pl	2009-04-12
pl	2009-04-13
pl	2009-05-01
pl	2009-05-03
pl	2009-05-31
pl	2009-06-11
pl	2009-08-15
pl	2009-11-01
pl	2009-11-11
pl	2009-12-25
pl	2009-12-26
hr	2010-01-01
hr	2010-01-06
hr	2010-04-05
hr	2010-05-01
hr	2010-06-03
hr	2010-06-22
hr	2010-06-25
hr	2010-08-05
hr	2010-08-15
hr	2010-10-08
hr	2010-11-01
hr	2010-12-25
hr	2010-12-26
hr	2009-01-01
hr	2009-01-06
hr	2009-04-13
hr	2009-05-01
hr	2009-06-11
hr	2009-06-22
hr	2009-06-25
hr	2009-08-05
hr	2009-08-15
hr	2009-10-08
hr	2009-11-01
hr	2009-12-25
hr	2009-12-26
hr	2008-01-01
hr	2008-01-06
hr	2008-03-24
hr	2008-05-01
hr	2008-05-22
hr	2008-05-25
hr	2008-06-22
hr	2008-06-25
hr	2008-08-05
hr	2008-08-15
hr	2008-10-08
hr	2008-11-01
hr	2008-12-25
hr	2008-12-26
hr	2007-01-01
hr	2007-01-06
hr	2007-04-09
hr	2007-05-01
hr	2007-06-07
hr	2007-06-22
hr	2007-06-25
hr	2007-08-05
hr	2007-08-15
hr	2007-10-08
hr	2007-11-01
hr	2007-12-25
hr	2007-12-26
#
cz	2004-01-01
cz	2004-04-12
cz	2004-05-01
cz	2004-05-08
cz	2004-07-05
cz	2004-07-06
cz	2004-09-28
cz	2004-10-28
cz	2004-11-17
cz	2004-12-24
cz	2004-12-25
cz	2004-12-26
cz	2005-01-01
cz	2005-03-28
cz	2005-05-01
cz	2005-05-08
cz	2005-07-05
cz	2005-07-06
cz	2005-09-28
cz	2005-10-28
cz	2005-11-17
cz	2005-12-24
cz	2005-12-25
cz	2005-12-26
cz	2006-01-01
cz	2006-04-17
cz	2006-05-01
cz	2006-05-08
cz	2006-07-05
cz	2006-07-06
cz	2006-09-28
cz	2006-10-28
cz	2006-11-17
cz	2006-12-24
cz	2006-12-25
cz	2006-12-26
cz	2007-01-01
cz	2007-04-09
cz	2007-05-01
cz	2007-05-08
cz	2007-07-05
cz	2007-07-06
cz	2007-09-28
cz	2007-10-28
cz	2007-11-17
cz	2007-12-24
cz	2007-12-25
cz	2007-12-26
sk	2004-01-01
sk	2004-01-06
sk	2004-04-09
sk	2004-04-12
sk	2004-05-01
sk	2004-05-08
sk	2004-07-05
sk	2004-08-29
sk	2004-09-01
sk	2004-09-15
sk	2004-11-01
sk	2004-12-24
sk	2004-12-25
sk	2004-12-26
sk	2005-01-01
sk	2005-01-06
sk	2005-03-25
sk	2005-03-28
sk	2005-05-01
sk	2005-05-08
sk	2005-07-05
sk	2005-08-29
sk	2005-09-01
sk	2005-09-15
sk	2005-11-01
sk	2005-11-17
sk	2005-12-24
sk	2005-12-25
sk	2005-12-26
sk	2006-01-01
sk	2006-01-06
sk	2006-04-14
sk	2006-04-17
sk	2006-05-01
sk	2006-05-08
sk	2006-07-05
sk	2006-08-29
sk	2006-09-01
sk	2006-09-15
sk	2006-11-17
sk	2006-11-01
sk	2006-12-24
sk	2006-12-25
sk	2006-12-26
sk	2007-01-01
sk	2007-01-06
sk	2007-04-06
sk	2007-04-09
sk	2007-05-01
sk	2007-05-08
sk	2007-07-05
sk	2007-08-29
sk	2007-09-01
sk	2007-09-15
sk	2007-11-01
sk	2007-11-17
sk	2007-12-24
sk	2007-12-25
sk	2007-12-26
sk	2008-01-01
sk	2008-01-06
sk	2008-03-21
sk	2008-03-24
sk	2008-05-01
sk	2008-05-08
sk	2008-07-05
sk	2008-08-29
sk	2008-09-01
sk	2008-09-15
sk	2008-11-01
sk	2008-11-17
sk	2008-12-24
sk	2008-12-25
sk	2008-12-26
#"""

def txt_data(text):
    temp = string.split(text,'\n')
    res = []
    for i in temp:
        if i[0] <> '#':
            res.append(i)
    return res

HOLIDAYS_DATES = txt_data(TXT_HOLIDAYS)
#print HOLIDAYS_DATES

def isHoliday(day='', countrycode=''):
    '''
    >>> isHoliday('2008-12-26', 'sk')
    True
    >>> isHoliday('2008-12-12', 'sk')
    False
    '''
    ish = 0
    for hday in HOLIDAYS_DATES:
        if ish == 0:
            if countrycode == hday[:2] and day == hday[-10:]:
                ish = 1
    if ish == 1:
        return True
    else:
        return False

def isWorkDay(isoday='', countrycode=''):
    ''' check whether it is weekend or holiday
    
    import pepe.p_datetime.holidays as hdays
    print hdays.weekendOrHoliday(<iso day>, 'sk')
    
    >>> isWorkDay('2008-12-26', 'sk')
    'holiday'
    >>> isWorkDay('2008-12-12', 'sk')
    'wd'
    '''
    res = ''
    if len(isoday) == 10 and len(countrycode) == 2:
        if isHoliday(isoday, countrycode) == True  and isWeekend(isoday) == False: res = 'holiday'
        if isHoliday(isoday, countrycode) == True  and isWeekend(isoday) == True:  res = 'holiday and weekend'
        if isHoliday(isoday, countrycode) == False and isWeekend(isoday) == True:  res = 'weekend'
        if isHoliday(isoday, countrycode) == False and isWeekend(isoday) == False: res = 'wd'
    return res

if __name__ == "__main__":
    ''' '''
    import doctest
    doctest.testmod()

    #print isWorkDay('2008-12-26', 'sk')
